home *** CD-ROM | disk | FTP | other *** search
- //: C18:Effector.txt
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
- // (Should be "cpp" but I can't get it to compile with
- // My windows compilers, so making it a txt file will
- // keep it out of the makefile for the time being)
- // Jerry Schwarz's "effectors"
- #include<iostream>
- #include <cstdlib>
- #include <string>
- #include <climits> // ULONG_MAX
- using namespace std;
-
- // Put out a portion of a string:
- class Fixw {
- string str;
- public:
- Fixw(const string& s, int width)
- : str(s, 0, width) {}
- friend ostream&
- operator<<(ostream& os, Fixw& fw) {
- return os << fw.str;
- }
- };
-
- typedef unsigned long ulong;
-
- // Print a number in binary:
- class Bin {
- ulong n;
- public:
- Bin(ulong nn) { n = nn; }
- friend ostream& operator<<(ostream&, Bin&);
- };
-
- ostream& operator<<(ostream& os, Bin& b) {
- ulong bit = ~(ULONG_MAX >> 1); // Top bit set
- while(bit) {
- os << (b.n & bit ? '1' : '0');
- bit >>= 1;
- }
- return os;
- }
-
- int main() {
- char* string =
- "Things that make us happy, make us wise";
- for(int i = 1; i <= strlen(string); i++)
- cout << Fixw(string, i) << endl;
- ulong x = 0xCAFEBABEUL;
- ulong y = 0x76543210UL;
- cout << "x in binary: " << Bin(x) << endl;
- cout << "y in binary: " << Bin(y) << endl;
- } ///:~
-